package com.dhh.sky.widget;

import java.util.ArrayList;
import java.util.List;
import com.dhh.sky.R;
import com.dhh.sky.activity.FolderChooseActivity;
import com.dhh.sky.model.SkyDriveObject;
import com.dhh.sky.util.FileManager;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FileChooseListAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private List<SkyDriveObject> files;  
	private FolderChooseActivity context;
	private FileManager fm;
	private String path;
	
	public FileChooseListAdapter(FolderChooseActivity context,String path){
		this.context = context;
		this.path = path;
		fm = new FileManager(context);
		inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		files = new ArrayList<SkyDriveObject>();  
	}
	
	public void loadFiles(){
		this.files.clear();
		for(SkyDriveObject file : fm.getFiles(path)){
			if(file.isFolder()){
				this.files.add(file);
			}
		}
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView != null ? convertView : inflater.inflate(R.layout.file_list_item,parent,false);
		
		SkyDriveObject file = this.getItem(position);
		ImageView icon = (ImageView) view.findViewById(R.id.fli_icon);
		TextView name = (TextView) view.findViewById(R.id.fli_name);
		TextView detail = (TextView) view.findViewById(R.id.fli_detail);
		view.findViewById(R.id.fli_check).setVisibility(View.INVISIBLE);
		name.setText(file.getName());
		icon.setImageResource(R.drawable.ic_nonemptydocumentfolder);
		detail.setText(file.getDetail());
		
		return view;
	}
	
	@Override
	public int getCount() {
		return files.size();
	}

	@Override
	public SkyDriveObject getItem(int position) {
		return files.get(position);
	}
	
	@Override
	public long getItemId(int position) {
		long id = -1;
		if(position>files.size()-1){
			id = Long.valueOf(files.get(position).getId());
		}
		return id;
	}
	
	public void setPath(String path) {
		this.path = path;
	}
}
